﻿$(function () {
    $personImage = $('#personImage');
    $personImage.lgbUploadFile();//上传图片控件
    $(' div.logoImage').addClass('input-group');
    var $echartsModal = $("#echartsModal");

    //设置出生日期不可为空
    $('#dataForm').autoValidate({
        personAge: {
            required: true
        }
    });

    var bsa = new BootstrapAdmin({
        url: PersonManage.url,
        dataEntity: new DataEntity({
            map: {
                ID: "personId",
                PersonName: "personName",
                PersonSex: "personSex",
                PersonAge: "personAge",
                PersonMajor: "personMajor",
                PersonImg: "personImage"
            }
        }),
        callback: function (data) {
            if (data.oper === 'create') {
                $('#personName').prop('readonly', false).removeClass("ignore");
                temp = 'create';
            }
            else if (data.oper === 'edit') {
                $('#personName').prop('readonly', true).addClass("ignore");
                temp = 'edit';
                //头像
                if (data.data.PersonImg != "") {
                    $personImage.attr('src', data.data.PersonImg);
                    $personImage.attr('data-file', data.data.PersonImg.substring(data.data.PersonImg.lastIndexOf('/') + 1));//通过图片文件名来获取图片
                    $('.logoImage').addClass('open');
                }
                //处理日期的显示问题
                var personAge = data.data.PersonAge.substring(0, 10);
                $('#personAge').val(personAge);
            }
            else if (data.oper === 'save') {
                $('#txt_expert_name').val('');
                $('table').bootstrapTable('refresh');
            }
        }
    });

    //关闭模态框事件
    $('#dialogNew').on('hide.bs.modal', function () {
        $personImage.attr('src', "");
        $personImage.attr('data-file', "");
        $('.logoImage').removeClass('open');
    });


    //表格数据加载
    $('table').smartTable({
        url: PersonManage.url,
        sortName: 'PersonName',
        queryParams: function (params) { return $.extend(params, { Name: $('#txt_expert_name').val() }); },
        columns: [{ checkbox: true },
            { title: "专家Id", field: "ID", events: bsa.idEvents(), formatter: BootstrapAdmin.idFormatter },
            { title: "学生姓名", field: "PersonName", sortable: true },
            { title: "性别", field: "PersonSex", sortable: false, formatter: function (value, row, index) { return (value || value == "true") ? "男" : "女"; } },
            { title: "年龄", field: "PersonAge", sortable: false, formatter: function (value, row, index) { return new Date().getFullYear() - new Date(value).getFullYear(); } },
            { title: "专业", field: "PersonMajor", sortable: false },
            { title: "头像", field: "PersonImg", align: 'center', sortable: false, formatter: function (value, row, index) { return '<img class="epIcon" src="' + value + '"/>' } }
        ]
    });

    //重置按钮
    $('#reset').on('click', function () {
        $('#txt_expert_name').val("");
        $('table').bootstrapTable('refreshOptions', { pageNumber: 1 });
    });

    $('.form_date').datetimepicker({
        language: 'zh-CN',
        weekStart: 1,
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,
        forceParse: 0,
        format: 'yyyy-mm-dd',
        pickerPosition: 'bottom-left'
    });
});